package zl.ordermanager.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zl.ordermanager.dao.PurchaseOrderDao;
import zl.ordermanager.po.Tpurchaseorder;
import zl.ordermanager.service.PurchaseOrderService;
import zl.ordermanager.vo.DataGrid;
import zl.ordermanager.vo.PageHelper;
import zl.ordermanager.vo.PurchaseOrder;
import zl.ordermanager.vo.Total;

@Service
public class PurchaseOrderServiceImpl implements PurchaseOrderService {

	@Autowired
	private PurchaseOrderDao purchaseOrderDao;

	@Override
	public DataGrid<PurchaseOrder> dataGrid(PageHelper pageHelper, Total total) {
		DataGrid<PurchaseOrder> dataGrid = new DataGrid<PurchaseOrder>();
		List<PurchaseOrder> purchaseOrderList = new ArrayList<PurchaseOrder>();
		Map<String, Object> params = new HashMap<String, Object>();
		if (pageHelper.getKeywords() != null && !pageHelper.getKeywords().isEmpty()) {
			params.put("keyword", "%%" + pageHelper.getKeywords() + "%%");
		}
		if (pageHelper.getCustomer() != null && !pageHelper.getCustomer().isEmpty()) {
			params.put("company", "%%" + pageHelper.getCustomer() + "%%");
		}
		if (pageHelper.getAbstractMonth() != null && !pageHelper.getAbstractMonth().isEmpty()) {
			params.put("orderDate", "%%" + pageHelper.getAbstractMonth() + "%%");
		}
		if (pageHelper.getInvoiceType() != null && !pageHelper.getInvoiceType().isEmpty()) {
			params.put("orderType", pageHelper.getInvoiceType());
		}
		String hql = " from Tpurchaseorder t ";
		List<Tpurchaseorder> tpurchaseorderList = purchaseOrderDao.find(hql + whereHql(pageHelper)
				+ orderHql(pageHelper), params, pageHelper.getPageNum(), pageHelper.getNumPerPage());
		if (tpurchaseorderList != null && tpurchaseorderList.size() > 0) {
			for (Tpurchaseorder tpurchaseorder : tpurchaseorderList) {
				PurchaseOrder purchaseOrder = new PurchaseOrder();
				BeanUtils.copyProperties(tpurchaseorder, purchaseOrder);
				purchaseOrderList.add(purchaseOrder);
			}
		}
		dataGrid.setList(purchaseOrderList);
		dataGrid.setTotal(purchaseOrderDao.count("select count(*) " + hql + whereHql(pageHelper), params));

		total.setMoney(purchaseOrderDao.sum("select sum(t.money) " + hql + whereHql(pageHelper), params));
		return dataGrid;
	}

	private String whereHql(PageHelper pageHelper) {
		String whereString = "";
		if (pageHelper.getKeywords() != null && !pageHelper.getKeywords().isEmpty()) {
			whereString += " where t.orderNo like :keyword ";
			whereString += " or t.orderDate like :keyword ";
			whereString += " or t.company like :keyword ";
			whereString += " or t.orderType like :keyword ";
			whereString += " or t.ai like :keyword ";
			whereString += " or t.money like :keyword ";
			whereString += " or t.invoice like :keyword ";
		}

		if (pageHelper.getCustomer() != null && !pageHelper.getCustomer().isEmpty()) {
			if (whereString.isEmpty()) {
				whereString = "where (t.company like :company)";
			} else {
				whereString += " and (t.company like :company)";
			}
		}
		if (pageHelper.getAbstractMonth() != null && !pageHelper.getAbstractMonth().isEmpty()) {
			if (whereString.isEmpty()) {
				whereString = "where (t.orderDate like :orderDate)";
			} else {
				whereString += " and (t.orderDate like :orderDate)";
			}
		}
		if (pageHelper.getInvoiceType() != null && !pageHelper.getInvoiceType().isEmpty()) {
			if (whereString.isEmpty()) {
				whereString = "where (t.orderType = :orderType)";
			} else {
				whereString += " and (t.orderType = :orderType)";
			}
		}

		return whereString;
	}

	private String orderHql(PageHelper pageHelper) {
		String orderString = "";
		if (pageHelper.getOrderField() != null && !pageHelper.getOrderField().isEmpty()
				&& pageHelper.getOrderDirection() != null && !pageHelper.getOrderDirection().isEmpty()) {
			orderString = " order by t." + pageHelper.getOrderField() + " " + pageHelper.getOrderDirection();
		}
		return orderString;
	}

	@Override
	public PurchaseOrder get(Integer id) {
		Tpurchaseorder tpurchaseorder = purchaseOrderDao.get(Tpurchaseorder.class, id);
		if (tpurchaseorder == null) {
			return null;
		}
		PurchaseOrder purchaseOrder = new PurchaseOrder();
		BeanUtils.copyProperties(tpurchaseorder, purchaseOrder);
		return purchaseOrder;
	}

	@Override
	public PurchaseOrder getByOrderNo(String orderNo) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("orderNo", orderNo);
		Tpurchaseorder tpurchaseorder = purchaseOrderDao
				.get("from Tpurchaseorder t where t.orderNo = :orderNo", params);
		if (tpurchaseorder == null) {
			return null;
		}
		PurchaseOrder purchaseOrder = new PurchaseOrder();
		BeanUtils.copyProperties(tpurchaseorder, purchaseOrder);
		return purchaseOrder;
	}

	@Override
	public void add(PurchaseOrder purchaseOrder) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("orderNo", purchaseOrder.getOrderNo());
		if (purchaseOrderDao.count("select count(*) from Tpurchaseorder t where t.orderNo = :orderNo", params) > 0) {
			throw new Exception("单据编号已存在！");
		}
		Tpurchaseorder tpurchaseorder = new Tpurchaseorder();
		BeanUtils.copyProperties(purchaseOrder, tpurchaseorder);
		purchaseOrderDao.save(tpurchaseorder);
	}

	@Override
	public void edit(PurchaseOrder purchaseOrder) throws Exception {
		Tpurchaseorder tpurchaseorder = purchaseOrderDao.get(Tpurchaseorder.class, purchaseOrder.getId());
		BeanUtils.copyProperties(purchaseOrder, tpurchaseorder);
	}

	@Override
	public void delete(Integer id) {
		purchaseOrderDao.delete(purchaseOrderDao.get(Tpurchaseorder.class, id));
	}

}
